package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.StockCategory;
import com.asset.salesforce.domain.StockSubCategory;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class StockCategoryDaoImpl extends BasicDaoImpl<StockCategory>{
		
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public StockCategory getByName(String name) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockCategory where name=  :name");
		 query.setParameter("name", name);
		 List<StockCategory>list  = new ArrayList<StockCategory>();
		 list = query.list();
		 if(list.size() >0){
			 return list.get(0);
		 }else{
			 return null;
		 }
		
	}

	@SuppressWarnings("unchecked")
	public StockCategory getByCode(String code) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockCategory where code=  :code");
		 query.setParameter("code", code);
		 List<StockCategory>list  = new ArrayList<StockCategory>();
		 list = query.list();
		 if(list.size() >0){
			 return list.get(0);
		 }else{
			 return null;
		 }
		
	}
	@SuppressWarnings("unchecked")
	public List<StockCategory> getAllCategoriesByEmployeeId(Integer employeeId) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockCategory where owner.id = :stockCategoryId");
		 query.setParameter("stockCategoryId", employeeId);
		 List<StockCategory>list  = new ArrayList<StockCategory>();
		 list = query.list();
		 if(list.size() >0){
			 return list;
		 }else{
			 return new ArrayList<StockCategory>();
		 }
	
	}
	
	public List<StockCategory> getAllStockCategoriesByCompanyId(Employee employee){
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockCategory stockCategory where stockCategory.owner.company.id = :companyId");
		 query.setParameter("companyId", employee.getCompany().getId());
		 List<StockCategory>list  = new ArrayList<StockCategory>();
		 list = query.list();
			 return list;
	}
}
